package com.lcc.lynx.jdbc.pojo;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lcc
 * @date 2024/12/3 18:05
 */
public class ParamMap extends LinkedHashMap<String, Object> {
    @Override
    public Object put(String key, Object value) {

        if(key.endsWith("InList")){
            if(value instanceof String){
                if(!value.toString().isEmpty()){
                    value = Arrays.asList(value.toString().split(","));
                    if(key.equals("orderByInList")){
                        value = ((List<String>) value).stream()
                                .map(item -> item.trim().replace(" asc", "")).toList();
                    }
                }else {
                    return null;
                }
            }
        }
        return super.put(key, value);
    }

    @Override
    public void putAll(Map<? extends String, ?> m) {
        for (Map.Entry<? extends String, ?> entry : m.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public Object putIfAbsent(String key, Object value) {
        if(!this.containsKey(key)){
            return put(key, value);
        }
        return null;
    }
}